#!/opt/miniconda3/bin/python
# -*- coding: utf-8 -*-

# Get the perpendicular projection of a point onto a line segment

import math

A = (0, 2)
B = (4, 0)
O = (1, 2)

OA = (A[0] - O[0], A[1] - O[1])
OB = (B[0] - O[0], B[1] - O[1])
AB = (B[0] - A[0], B[1] - A[1])

one_degree = 0.017453292519943295
ninty_degree = 0.5 * math.pi

def cosv(v1, v2):
    return (v1[0] * v2[0] + v1[1] * v2[1]) / (math.sqrt(v1[0]**2 + v1[1]**2) * math.sqrt(v2[0]**2 + v2[1]**2))

def length(v):
    return math.sqrt(v[0]**2 + v[1]**2)

ang_A = math.acos(cosv(OA, AB))
ang_B = math.acos(cosv(OB, AB))
ang_A_diff = abs(ang_A - ninty_degree)
ang_B_diff = abs(ang_B - ninty_degree)

cos_v = 0
while ang_A_diff>one_degree and ang_B_diff>one_degree:
    if length(OA) > length(OB):
        A = ((A[0]+B[0])/2, (A[1]+B[1])/2)
        OA = (A[0] - O[0], A[1] - O[1])
        ang_A = math.acos(cosv(OA, AB))
        ang_A_diff = abs(ang_A - ninty_degree)
        cos_v = cosv(OA, AB)
    else:
        B = ((A[0]+B[0])/2, (A[1]+B[1])/2)
        OB = (B[0] - O[0], B[1] - O[1])
        ang_B = math.acos(cosv(OB, AB))
        ang_B_diff = abs(ang_B - ninty_degree)
        cos_v = cosv(OB, AB)
    print("%.4f, %.4f, %.4f, %.4f, %.4f" % (one_degree, ang_A, ang_B, ang_A_diff, ang_B_diff))

print("%.4f, %.4f" % (cos_v, math.acos(cos_v) * 180 / math.pi), end="\n")